package com.junjiao.util.java.email;

import java.io.File;
import java.util.Properties;
import com.junjiao.util.java.PropertyUtil;

/**
 * @author wgz
 * 
 */
public class EmailTools {

	private SendMail sendMail = null;
	private MailInfo mailInfo = null;
	private Properties pro = PropertyUtil.getProperties("email.properties");
	private static EmailTools tools = null;

	private EmailTools() {
		try {
			mailInfo = new MailInfo();
			mailInfo.setHost(pro.getProperty("email.host").trim());
			mailInfo.setUserName(pro.getProperty("email.username").trim());
			mailInfo.setPassword(pro.getProperty("email.password").trim());
			mailInfo.setSslPort(Integer.parseInt(pro
					.getProperty("email.sslPort")));

			this.sendMail = new SendMail(mailInfo.getHost(), mailInfo
					.getUserName(), mailInfo.getPassword(), mailInfo
					.getSslPort());
		} catch (Exception e) {
			e.printStackTrace();
		}
	};

	public static EmailTools newInstance() {
		if (tools == null) {
			synchronized (EmailTools.class) {
				if (tools == null) {
					tools = new EmailTools();
				}
			}
		}
		return tools;
	}

	/**
	 * 初始化邮件发送服务器
	 * 
	 * @param host
	 *            邮件发送服务器
	 * @param userName
	 *            用户名
	 * @param password
	 *            密码
	 * @param sslPort
	 *            端口
	 * @return
	 */
	public EmailTools setEmailTools(String host, String userName,
			String password, int sslPort) {

		try {
			mailInfo = new MailInfo();
			mailInfo.setHost(host);
			mailInfo.setUserName(userName);
			mailInfo.setPassword(password);
			mailInfo.setSslPort(sslPort);

			this.sendMail = new SendMail(mailInfo.getHost(), mailInfo
					.getUserName(), mailInfo.getPassword(), mailInfo
					.getSslPort());

		} catch (Exception e) {
			e.printStackTrace();
		}

		return this;
	}

	public EmailTools setEmailTools(MailInfo emailInfo) {

		try {
			mailInfo = new MailInfo();
			mailInfo.setHost(emailInfo.getHost());
			mailInfo.setUserName(emailInfo.getUserName());
			mailInfo.setPassword(emailInfo.getPassword());
			mailInfo.setSslPort(emailInfo.getSslPort());

			this.sendMail = new SendMail(mailInfo.getHost(), mailInfo
					.getUserName(), mailInfo.getPassword(), mailInfo
					.getSslPort());

		} catch (Exception e) {
			e.printStackTrace();
		}
		return this;
	}

	public void sendSimpleMail(String content, String mainSubject, int type)
			throws Exception {
		if (sendMail == null) {
			throw new NullPointerException("EmailTools.sendMail is null!");
		}
		sendMail.send(sendMail.createMimeMessage(mainSubject, pro.getProperty(
				"email.to").trim().split("\\|\\|\\|"), pro.getProperty(
				"email.sslPort").trim(), content, type));
	}

	public void sendSimpleMail(String content) throws Exception {
		if (sendMail == null) {
			throw new NullPointerException("EmailTools.sendMail is null!");
		}
		sendMail.send(sendMail.createMimeMessage(pro.getProperty(
				"email.subject").trim(), pro.getProperty("email.to").trim()
				.split("\\|\\|\\|"), pro.getProperty("email.sslPort").trim(),
				content, SendMail.E_PLAIN));
	}

	/**
	 * 发送邮件
	 * 
	 * @param content
	 *            邮件内容
	 * @param mainSubject
	 *            标题
	 * @param to
	 *            收件人列表
	 * @param from
	 *            发件人
	 * @param type
	 *            邮件类型
	 * @throws Exception
	 */
	public void sendSimpleMail(String content, String mainSubject, String[] to,
			String from, int type) throws Exception {

		if (sendMail == null) {
			throw new NullPointerException("EmailTools.sendMail is null!");
		}
		sendMail.send(sendMail.createMimeMessage(mainSubject, to, from,
				content, type));
	}

	public void sendSimpleMail(MailInfo emailInfo) throws Exception {

		if (mailInfo == null) {
			throw new NullPointerException("EmailTools.mailInfo is null!");
		}

		mailInfo.setSubject(emailInfo.getSubject());
		mailInfo.setContent(emailInfo.getContent());
		mailInfo.setAtaches(emailInfo.getAtaches());
		mailInfo.setFrom(emailInfo.getFrom());
		mailInfo.setTo(emailInfo.getTo());
		mailInfo.setType(emailInfo.getType());

		if (mailInfo.getAtaches() != null && mailInfo.getAtaches().length > 0) {
			sendMail.send(sendMail.createMimeMessage(mailInfo.getSubject(),
					mailInfo.getTo(), mailInfo.getFrom(),
					mailInfo.getContent(), mailInfo.getAtaches(), mailInfo
							.getType()));
		} else {
			sendMail.send(sendMail.createMimeMessage(mailInfo.getSubject(),
					mailInfo.getTo(), mailInfo.getFrom(),
					mailInfo.getContent(), mailInfo.getType()));
		}
	}

	public void sendSimpleMail(String content, String mainSubject, String[] to,
			String from, File[] attachfiles, int type) throws Exception {

		if (sendMail == null) {
			throw new NullPointerException("EmailTools.sendMail is null!");
		}
		sendMail.send(sendMail.createMimeMessage(mainSubject, to, from,
				content, attachfiles, type));
	}

	public void sendSimpleMail(String content, String mainSubject,
			File[] attachfiles, int type) throws Exception {

		if (sendMail == null) {
			throw new NullPointerException("EmailTools.sendMail is null!");
		}
		sendMail.send(sendMail.createMimeMessage(mainSubject, pro.getProperty(
				"email.to").trim().split("\\|\\|\\|"), pro.getProperty(
				"email.sslPort").trim(), content, attachfiles, type));
	}

	public void sendSimpleMail(String content, File[] attachfiles)
			throws Exception {

		if (sendMail == null) {
			throw new NullPointerException("EmailTools.sendMail is null!");
		}
		sendMail.send(sendMail.createMimeMessage(pro.getProperty(
				"email.subject").trim(), pro.getProperty("email.to").trim()
				.split("\\|\\|\\|"), pro.getProperty("email.sslPort").trim(),
				content, attachfiles, SendMail.E_PLAIN));
	}

	public MailInfo getMailInfo() {
		return mailInfo;
	}

	public void setMailInfo(MailInfo mailInfo) {
		this.mailInfo = mailInfo;
	}

	public static void main(String args[]) {

		String host = "mail.besttone.com.cn";
		String userName = "jiaojun";
		String password = "15991033667";
		int sslPort = 0;
		EmailTools tools = new EmailTools().setEmailTools(host, userName,
				password, sslPort);

		try {
			String content = "恭喜您，您可带领您所购买的苹果手机，就近兑换10000元人民币！";
			String mainSubject = "中奖通知";
			String[] to = new String[] { "junjiao.j@gmail.com" };
			String from = "zhanggaobin@besttone.com.cn";
			tools.sendSimpleMail(content, mainSubject, to, from,
					SendMail.E_PLAIN);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
